package cn.com.nes.site.controller.monitor;

import cn.com.nes.common.exception.MissRequestParameterException;
import cn.com.nes.site.controller.monitor.vo.QueryEquipmentVO;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.service.monitor.EquipmentService;
import cn.com.nes.site.service.monitor.dto.EquipmentDTO;
import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/v2/equipment")
public class EquipmentController {

    @Resource
    EquipmentService equipmentService;

    /**
     * 通过传输序列号查询设备
     * @param dtuId        传输序列号
     * @param dtuQueryType 传输序列号查询方式 必填： like-模糊查询，eq-精准查询
     * @return
     */
    @RequestMapping(value = "getEquipmentsByDtu", method = RequestMethod.GET)
    @ResponseBody
    public PageData getEquipmentsByDtu(@RequestParam("dtuId") String dtuId,
            @RequestParam("dtuQueryType") String dtuQueryType) throws Exception {
        QueryEquipmentVO vo = new QueryEquipmentVO();
        vo.setDtuId(dtuId);
        vo.setDtuQueryType(dtuQueryType);
        List<EquipmentDTO> equipmentList = equipmentService.getEquipmentList(vo.convertToEquipmentBO());
        return PageData.ok("equipments", equipmentList);
    }

    /**
     * 获取设备列表
     * @return
     */
    @RequestMapping(value = "getEquipments", method = RequestMethod.GET)
    @ResponseBody
    public PageData getEquipments(@Validated QueryEquipmentVO vo) throws Exception {
        if (StrUtil.isNotEmpty(vo.getDtuId()) && StrUtil.isEmpty(vo.getDtuQueryType())) {
            throw new MissRequestParameterException("传输序列号查询方式");
        }
        List<EquipmentDTO> equipmentList = equipmentService.getEquipmentList(vo.convertToEquipmentBO());
        return PageData.ok("equipments", equipmentList);
    }

}
